package cn.sdormitory.smartdor.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.sdormitory.smartdor.dao.RepairDeviceDao;
import cn.sdormitory.smartdor.entity.RepairDevice;
import cn.sdormitory.smartdor.service.RepairDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建人：zhouyang
 * @创建时间：2021/10/28 11:40
 * @version：V1.0
 */
@Service("repairDeviceService")
public class RepairDeviceServiceImpl extends ServiceImpl<RepairDeviceDao, RepairDevice> implements RepairDeviceService {
    @Override
    public List<Long> listDeviceIdByRepairId(Long repairId) {
        List<Long> ids = new ArrayList<>();
        List<RepairDevice> repairDevices = this.baseMapper.selectList(new LambdaQueryWrapper<RepairDevice>().eq(RepairDevice::getRepairId, repairId));
        if (CollectionUtil.isNotEmpty(repairDevices)) {
            repairDevices.forEach(item -> {
                ids.add(item.getDeviceId());
            });
            return ids;
        }
        return null;
    }

    @Override
    public void insertRepairAndRepairDevice(Long repairId, List<Long> deviceIdList) {
        this.baseMapper.insertRepairAndRepairDevice(repairId,deviceIdList);
    }

    @Override
    public void delAndCreateDevice(Long repairId, List<Long> deviceIdList) {
        remove(new LambdaQueryWrapper<RepairDevice>().eq(RepairDevice::getRepairId, repairId));
        if (CollUtil.isNotEmpty(deviceIdList)) {
            //保存报修与设备关系
            insertRepairAndRepairDevice(repairId, deviceIdList);
        }
    }
}
